package com.warwithinme.android.widgetwar;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;

public class ButtonWifi extends TButton
{

	private static final String INTENT_ACTION = "com.warwithinme.android.widgetwar.ButtonWifi";
	private Intent _intent;

	public int getButtonBmp(Context context)
	{
		if (_intent != null)
		{
			int state = _intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
			_intent = null;
			if (state == WifiManager.WIFI_STATE_ENABLED)
				return R.drawable.wifi_on;
			else
				return R.drawable.wifi_off;
		}

		//If this method is call when not receiving a intent from android system,
		//Then invoke this block.
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		if (!wifiManager.isWifiEnabled())
		{
//			Log.v("armNclover", "wifi is disabled.");
			return R.drawable.wifi_off;
		} else
		{
//			Log.v("armNclover", "wifi is enabled.");
			return R.drawable.wifi_on;
		}
	}

	@Override
	public void toggleSetting(Context context)
	{
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		boolean enabled = !wifiManager.isWifiEnabled();
		wifiManager.setWifiEnabled(enabled);
	}

	@Override
	public String getIntentAction()
	{
		return INTENT_ACTION;
	}

	@Override
	public int canHandleIntent(Intent intent)
	{
		if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION))
		{
			_intent = intent;
			return ToggleWidget.HANDLING_TYPE_DONE;
//		}else if(intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED))
//		{
//			return ToggleWidget.HANDLING_TYPE_SEQUENTIAL;
		}else
		{
			return ToggleWidget.HANDLING_TYPE_UNABLE;
		}
	}

}
